<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    输入消息<input type="text" id="val">
    <button id="send">发送</button>
    <div id="msg"></div>
</div>

<script type="text/javascript" src="https://cdn.bootcss.com/socket.io/2.2.0/socket.io.js"></script>
<script>
  window.onload = function () {
    const socket = io('ws://localhost:5000')
    var btn = document.getElementById('send')
    var input = document.getElementById('val')
    var msg = document.getElementById('msg')
      var text = ''
      input.onkeyup = function () {
          text = this.value
      }
        const id = socket.id
      socket.on('receive', function (data) {
          msg.innerHTML += `<p>${data}</p>`
      })

      btn.onclick = function () {
          socket.emit('send', text)

          input.value = ''
      }
  }
</script>
</body>
</html>